<?php

namespace App\Services;

use Mrgoon\AliSms\AliSms;

/**
 * 阿里云短信类
 */
class AliyunSmsService
{
    //模板CODE
    public static $templateCodes = [
        'VERIFICATION_CODE' => 'SMS_474931230'
    ];


    /**
     * 发送短信
     */
    public static function sendSms($mobile, $template_code, $params = [])
    {
        if (empty($mobile)) {
            return failureMessage('手机号不能为空！');
        }

        try {
            $ali_sms = new AliSms();
            $response = $ali_sms->sendSms($mobile, $template_code, $params);

            if ($response->Code == 'isv.BUSINESS_LIMIT_CONTROL') {
                if (strpos($response->Message,'1') !== FALSE) {
                    return failureMessage('1分钟后尝试！');
                }

                if (strpos($response->Message,'5') !== FALSE) {
                    return failureMessage('5分钟后尝试！');
                }
            }

            if ($response->Code == 'OK') {
                return successMessage();
            }

            return failureMessage($response->Message);
        } catch (\Throwable $e) {
            return failureMessage($e->getMessage());
        }
    }

}